package com.orchard.xpz.service.sysUser.impl;


import com.orchard.xpz.entity.sysUser.SysUser;
import com.orchard.xpz.dao.SysUserDao;
import com.orchard.xpz.service.sysUser.SysUserService;
import com.orchard.xpz.utils.PageResultData;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/14 14:21
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public PageResultData<SysUser> getPageList(SysUser sysUser) {
        PageHelper.startPage(sysUser.getPageNum(),sysUser.getPageSize());
        List<SysUser> userList = sysUserDao.getListUser(sysUser);
        PageResultData<SysUser> pageResultData = new PageResultData<>(userList,sysUser.getPageNum(),sysUser.getPageSize());
        return pageResultData;
    }

    @Override
    public SysUser getUserByName(String userName) {
        SysUser sysUser = sysUserDao.selectByName(userName);
        return sysUser;
    }

    @Override
    public boolean saveOrUpdate(SysUser user) {

        if(ObjectUtils.isNotEmpty(user.getUserId())){ //更新
            user.setUpdateTime(LocalDate.now());
            int i = sysUserDao.updateByPrimaryKeySelective(user);
            return i > 0? true : false;
        }else{
            user.setCreateTime(LocalDate.now());
            int i = sysUserDao.insertSelective(user);
            return i > 0? true : false;
        }
    }
}
